package com.example.messagemodule.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import java.util.LinkedList;
import java.util.Random;

public class AudioWaveView extends View {
    private Paint paint;
    private RectF rectF1;
    private RectF rectF2;
    private RectF rectF3;
    private RectF rectF4;
    private RectF rectF5;
    private RectF rectF6;
    private RectF rectF7;
    private RectF rectF8;
    private RectF rectF9;
    private RectF rectF10;
    private RectF rectF11;
    private RectF rectF12;
    private RectF rectF13;
    private RectF rectF14;
    private RectF rectF15;
    private RectF rectF16;
    private RectF rectF17;
    private RectF rectF18;
    private RectF rectF19;
    private RectF rectF20;
    private int viewWidth;
    private int viewHeight;
    /** 每个条的宽度 */
    private int rectWidth;
    /** 条数 */
    private int columnCount = 20;
    /** 条间距 */
    private final int space = 6;
    /** 条随机高度 */
    private LinkedList<Integer>
            randomHeight = new LinkedList<>();;
    private Random random;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            randomHeight.removeFirst();
            randomHeight.addLast(random.nextInt(viewHeight));
            invalidate();
        }
    };

    public AudioWaveView(Context context) {
        super(context);
        init();
    }

    public AudioWaveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        viewHeight = MeasureSpec.getSize(heightMeasureSpec);

        rectWidth = 5;
    }

    private void init() {
        for (int i = 0; i < columnCount; i++) {
            randomHeight.addFirst(30);
        }
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        random = new Random();
        initRect();
    }

    private void initRect() {
        rectF1 = new RectF();
        rectF2 = new RectF();
        rectF3 = new RectF();
        rectF4 = new RectF();
        rectF5 = new RectF();
        rectF6 = new RectF();
        rectF7 = new RectF();
        rectF8 = new RectF();
        rectF9 = new RectF();
        rectF10 = new RectF();
        rectF11 = new RectF();
        rectF12 = new RectF();
        rectF13 = new RectF();
        rectF14 = new RectF();
        rectF15 = new RectF();
        rectF16 = new RectF();
        rectF17 = new RectF();
        rectF18 = new RectF();
        rectF19 = new RectF();
        rectF20 = new RectF();
    }

    public void setData(int firstData){
        randomHeight.removeFirst();
        if (firstData<10){
            randomHeight.addLast(10);
        }else{
            randomHeight.addLast(firstData);
        }
    }

    public void notifityChangeData(){
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int left = rectWidth + space;
        rectF1.set(left * 20, (viewHeight-randomHeight.get(0))/2, left * 20 + rectWidth, randomHeight.get(0)+(viewHeight-randomHeight.get(0))/2);
        rectF2.set(left * 19, (viewHeight-randomHeight.get(1))/2, left * 19 + rectWidth, randomHeight.get(1)+(viewHeight-randomHeight.get(1))/2);
        rectF3.set(left * 18, (viewHeight-randomHeight.get(2))/2, left * 18 + rectWidth, randomHeight.get(2)+(viewHeight-randomHeight.get(2))/2);
        rectF4.set(left * 17, (viewHeight-randomHeight.get(3))/2, left * 17 + rectWidth, randomHeight.get(3)+(viewHeight-randomHeight.get(3))/2);
        rectF5.set(left * 16, (viewHeight-randomHeight.get(4))/2, left * 16 + rectWidth, randomHeight.get(4)+(viewHeight-randomHeight.get(4))/2);
        rectF6.set(left * 15, (viewHeight-randomHeight.get(5))/2, left * 15 + rectWidth, randomHeight.get(5)+(viewHeight-randomHeight.get(5))/2);
        rectF7.set(left * 14, (viewHeight-randomHeight.get(6))/2, left * 14 + rectWidth, randomHeight.get(6)+(viewHeight-randomHeight.get(6))/2);
        rectF8.set(left * 13, (viewHeight-randomHeight.get(7))/2, left * 13 + rectWidth, randomHeight.get(7)+(viewHeight-randomHeight.get(7))/2);
        rectF9.set(left * 12, (viewHeight-randomHeight.get(8))/2, left * 12 + rectWidth, randomHeight.get(8)+(viewHeight-randomHeight.get(8))/2);
        rectF10.set(left * 11, (viewHeight-randomHeight.get(9))/2, left * 11 + rectWidth, randomHeight.get(9)+(viewHeight-randomHeight.get(9))/2);
        rectF11.set(left * 10, (viewHeight-randomHeight.get(10))/2, left * 10 + rectWidth, randomHeight.get(10)+(viewHeight-randomHeight.get(10))/2);
        rectF12.set(left * 9, (viewHeight-randomHeight.get(11))/2, left * 9 + rectWidth, randomHeight.get(11)+(viewHeight-randomHeight.get(11))/2);
        rectF13.set(left * 8, (viewHeight-randomHeight.get(12))/2, left * 8 + rectWidth, randomHeight.get(12)+(viewHeight-randomHeight.get(12))/2);
        rectF14.set(left * 7, (viewHeight-randomHeight.get(13))/2, left * 7 + rectWidth, randomHeight.get(13)+(viewHeight-randomHeight.get(13))/2);
        rectF15.set(left * 6, (viewHeight-randomHeight.get(14))/2, left * 6 + rectWidth, randomHeight.get(14)+(viewHeight-randomHeight.get(14))/2);
        rectF16.set(left * 5, (viewHeight-randomHeight.get(15))/2, left * 5 + rectWidth, randomHeight.get(15)+(viewHeight-randomHeight.get(15))/2);
        rectF17.set(left * 4, (viewHeight-randomHeight.get(16))/2, left * 4 + rectWidth, randomHeight.get(16)+(viewHeight-randomHeight.get(16))/2);
        rectF18.set(left * 3, (viewHeight-randomHeight.get(17))/2, left * 3 + rectWidth, randomHeight.get(17)+(viewHeight-randomHeight.get(17))/2);
        rectF19.set(left * 2, (viewHeight-randomHeight.get(18))/2, left * 2 + rectWidth, randomHeight.get(18)+(viewHeight-randomHeight.get(18))/2);
        rectF20.set(left * 1, (viewHeight-randomHeight.get(19))/2, left * 1 + rectWidth, randomHeight.get(19)+(viewHeight-randomHeight.get(19))/2);

        canvas.drawRect(rectF1, paint);
        canvas.drawRect(rectF2, paint);
        canvas.drawRect(rectF3, paint);
        canvas.drawRect(rectF4, paint);
        canvas.drawRect(rectF5, paint);
        canvas.drawRect(rectF6, paint);
        canvas.drawRect(rectF7, paint);
        canvas.drawRect(rectF8, paint);
        canvas.drawRect(rectF9, paint);
        canvas.drawRect(rectF10, paint);
        canvas.drawRect(rectF11, paint);
        canvas.drawRect(rectF12, paint);
        canvas.drawRect(rectF13, paint);
        canvas.drawRect(rectF14, paint);
        canvas.drawRect(rectF15, paint);
        canvas.drawRect(rectF16, paint);
        canvas.drawRect(rectF17, paint);
        canvas.drawRect(rectF18, paint);
        canvas.drawRect(rectF19, paint);
        canvas.drawRect(rectF20, paint);

//        handler.sendEmptyMessageDelayed(0, 100); //每间隔200毫秒发送消息刷新
    }

}
